/**
 * 
 */
package com.gr.staffpm.pages.dashboard.gadgets;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;
import org.wicketstuff.flot.Color;
import org.wicketstuff.flot.LegendPosition;

import com.gr.staffpm.charts.FlotPanel;
import com.gr.staffpm.charts.FlotUtils;
import com.gr.staffpm.charts.PredefinedCharts;
import com.gr.staffpm.charts.Series;
import com.gr.staffpm.datatypes.Feedback;
import com.gr.staffpm.feedback.service.FeedbackService;
import com.gr.staffpm.security.service.UserService;


/**
 * @author Graham Rhodes 6 Apr 2011 10:26:55
 */
public class RecentFeedbackGadget extends Panel {

    private static final long serialVersionUID = 1L;
    @SpringBean(name = "feedbackService")
    private FeedbackService feedbackService;
    @SpringBean(name = "userService")
    private UserService userService;

    public RecentFeedbackGadget(String id) {
        super(id);

        FlotPanel panel = new FlotPanel("chart", 360, 300, new PropertyModel<List<Series>>(this, "data"));
        panel.setLegendPosition(LegendPosition.NORTHWEST);
        panel.setLegendColumns(2);
        panel.setAxisTicksX(FlotUtils.getTicksForWeeksBetween(new DateTime().withTime(0, 0, 0, 0).minusDays(30), 5));
        panel.setAxisTimeModeX(true);
        panel.setAxisMinX(new DateTime().withTime(0, 0, 0, 0).minusDays(30));
        panel.setAxisMaxX(new DateTime().withTime(0, 0, 0, 0));
        panel.setAxisMinY(0.0);
        panel.setAxisMinTimeTickSizeX(1, "month");
        panel.setPointsVisible(true);
        add(panel);

    }

    public List<Series> getData() {
        DateTime start = new DateTime().withTime(0, 0, 0, 0).minusDays(30);
        List<List<Feedback>> stars = new ArrayList<List<Feedback>>();
        List<Color> colors = new ArrayList<Color>();
        colors.add(new Color(0xE6, 0x16, 0x16));
        colors.add(new Color(0xFF, 0x66, 0x00));
        colors.add(new Color(0x00, 0xCC, 0xFF));
        colors.add(new Color(0x00, 0x00, 0xFF));
        colors.add(new Color(0xFF, 0xCC, 0x00));
        colors.add(new Color(0x00, 0x99, 0x00));
        for (int day = 0; day <= 30; day++) {
            List<Feedback> feedbackForDay = new ArrayList<Feedback>();
            for (int i = 0; i < 6; i++) {
                feedbackForDay.addAll(feedbackService.getFeedbackForUserBetweenDatesWithRating(userService.getCurrentUser(), start,
                        start.withTime(start.hourOfDay().getMaximumValue(), 59, 59, 59), i));
            }
            stars.add(feedbackForDay);
            start = start.plusDays(1);
        }
        return PredefinedCharts.recentFeedback(stars, colors, 30);
    }

}
